package Gui.Dialogs;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FontMetrics;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;

import GameFiles.GameManager;
import Gui.GameFrame;

public class NumberOfMonthsDialog extends JDialog {
	/**
	 * 
	 */
	private int m_MaxMonthsNumbers=13;

	public NumberOfMonthsDialog(JFrame frame) {
		super(frame, "How Many Months?",true);
		setLayout(new GridLayout(0,3,3,0));
		setLocationRelativeTo(frame);
		
		setButtons();
	
		pack();
		// REFACTOR THIS! MAGIC NUMBER
		if(getTitle() != null){
			FontMetrics fm = getFontMetrics(getFont());
			int width = fm.stringWidth(getTitle()) + 75; // MAGIC NUMBER, 75 is size of a "typical" window icon
			width = (int) Math.max(width*1.2, getPreferredSize().getWidth());
			setSize(new Dimension(width, getHeight()));
		}
		setVisible(true);
	}

	private void setButtons(){
		for(int i=1;i<m_MaxMonthsNumbers;i++){
			JButton a_button=(JButton) new JButton(" "+i);
			a_button.addActionListener(NumberButtonActions(i));
			this.add(a_button);
		}

	}
	private ActionListener NumberButtonActions(final int num){
		return new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				setNumberOfMonths(num);
				dispose();
				GameFrame.getInstance().update();
				System.out.println("Month Number is "+num);
			}
		};
	}

	private void setNumberOfMonths(int a_num) {
		GameManager.getInstance().setNumberOfMonths(a_num);
	}
}
